package com.video.list.player

import android.net.Uri
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.MediaMetadata
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.analytics.AnalyticsListener
import com.google.android.exoplayer2.source.ConcatenatingMediaSource
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.PlayerView
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util

class VideoManager private constructor(){

    companion object{
        val instance: VideoManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
            VideoManager()
        }
    }

    val mSimpleExoPlayer by lazy { SimpleExoPlayer.Builder(MyApp.context!!).build() }
    private val concatMedia = ConcatenatingMediaSource()
    val playerView by lazy {
        View.inflate(
            MyApp.context!!,
            R.layout.view_exo_concat_video_play,
            null
        ) as PlayerView
    }

    val mVideoListenerList = arrayListOf<VideoCallBackListener>()

    fun initExoPlayer(){
        //循环模式
        mSimpleExoPlayer.addAnalyticsListener(object : AnalyticsListener {
            override fun onRenderedFirstFrame(
                eventTime: AnalyticsListener.EventTime,
                output: Any,
                renderTimeMs: Long
            ) {
                super.onRenderedFirstFrame(eventTime, output, renderTimeMs)

                for (element in mVideoListenerList){
                    element.onRenderedFirstFrame(eventTime,output,renderTimeMs)
                }
            }
        })

        mSimpleExoPlayer.repeatMode = Player.REPEAT_MODE_ONE
        playerView.player = mSimpleExoPlayer
        mSimpleExoPlayer.setMediaSource(concatMedia)
        mSimpleExoPlayer.prepare()
        mSimpleExoPlayer.play()
    }

    fun addMedialSource(url: String?){
        concatMedia.addMediaSource(getMediaSource(url))
    }

    private fun getMediaSource(url: String?): MediaSource {
        // 在播放期间测量带宽。如果不需要，可以为空。
        val bandwidthMeter = DefaultBandwidthMeter.Builder(MyApp.context!!).build()
        // 生成用于加载媒体数据的数据源实例。
        val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
            MyApp.context!!,
            Util.getUserAgent(MyApp.context!!, MyApp.context!!.packageName), bandwidthMeter
        )
        // 这是媒体资源，代表要播放的媒体。
        val mUri = Uri.parse(url)
        val builder = MediaItem.Builder()
        val mediaMetadata = MediaMetadata.Builder()
        builder.setMediaMetadata(mediaMetadata.build())
        val mediaItem = builder.setUri(mUri).build()
        return ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem)
    }


}